package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.util.Rediskeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class StrategyStatisVORedisServiceImpl implements IStrategyStatisVORedisService {

    @Autowired
    private StringRedisTemplate template;
    @Autowired
    private IStrategyService strategyService;

    @Override
    public void savePhonekey(String phone, String code) {
        String key = Rediskeys.VERIFY_CODE.join(phone);
        template.opsForValue().set(key, code, Rediskeys.VERIFY_CODE.getTime(), TimeUnit.SECONDS);
    }

    @Override
    public String getVerifyCode(String phone) {
        String key = Rediskeys.VERIFY_CODE.join(phone);
        return template.opsForValue().get(key);
    }

    @Override
    public String setToken(UserInfo userInfo) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String key = Rediskeys.TOKEN_CODE.join(uuid);
        String value = JSON.toJSONString(userInfo);
        template.opsForValue().set(key, value, Rediskeys.TOKEN_CODE.getTime(), TimeUnit.SECONDS);
        return key;
    }

    @Override
    public StrategyStatisVO getToken(String sid) {
        if (!StringUtils.hasLength(sid)) {
            return null;
        }
        String token = Rediskeys.STRATEGY_STATIS_VO.join(sid);
        StrategyStatisVO strategyStatisVO = new StrategyStatisVO();
        if (!template.hasKey(token)) {
            Strategy strategy = strategyService.get(sid);
            BeanUtils.copyProperties(strategy, strategyStatisVO);
            strategyStatisVO.setStrategyId(strategy.getId());
            template.opsForValue().set(token, JSON.toJSONString(strategy));
        } else {
            String s = template.opsForValue().get(token);
            strategyStatisVO = JSON.parseObject(s, StrategyStatisVO.class);
        }
        return strategyStatisVO;
    }

    @Override
    public void viewnumIncrease(String sid) {
        String token = Rediskeys.STRATEGY_STATIS_VO.join(sid);
        StrategyStatisVO strategyStatisVO = this.getToken(sid);
        strategyStatisVO.setViewnum(strategyStatisVO.getViewnum() + 1);
        template.opsForValue().set(token, JSON.toJSONString(strategyStatisVO));
    }

    @Override
    public void commentNumIncrease(String sid) {
        String token = Rediskeys.STRATEGY_STATIS_VO.join(sid);
        StrategyStatisVO strategyStatisVO = this.getToken(sid);
        strategyStatisVO.setReplynum(strategyStatisVO.getReplynum() + 1);
        template.opsForValue().set(token, JSON.toJSONString(strategyStatisVO));
    }

    @Override
    public Object thumbsupNumIncrease(String sid, UserInfo userInfo) {
        String token = Rediskeys.STRATEGY_STATIS_VO.join(sid);
        String key = Rediskeys.THUMBSUP_CODE.join(userInfo.getId(), sid);
        if (StringUtils.hasLength(template.opsForValue().get(key))) {
            return null;
        }
        StrategyStatisVO strategyStatisVO = this.getToken(sid);
        strategyStatisVO.setThumbsupnum(strategyStatisVO.getThumbsupnum() + 1);
        template.opsForValue().set(token, JSON.toJSONString(strategyStatisVO));

        template.opsForValue().set(key, "123", Rediskeys.THUMBSUP_CODE.getTime(), TimeUnit.MINUTES);
        return "true";
    }

    @Override
    public Object favorNumIncrease(String sid, UserInfo userInfo) {
        String token = Rediskeys.STRATEGY_STATIS_VO.join(sid);
        StrategyStatisVO strategyStatisVO = this.getToken(sid);
        String key = Rediskeys.FAVOR_USER_CODE.join(userInfo.getId());
//        System.out.println("——————————————————————————————————————————————————————————————————————");
        List<String> sidList = new ArrayList<>();
        if (!template.hasKey(key)) {
            sidList.add(sid);
            template.opsForValue().set(key, JSON.toJSONString(sidList));
            strategyStatisVO.setFavornum(strategyStatisVO.getFavornum() + 1);
            template.opsForValue().set(token, JSON.toJSONString(strategyStatisVO));
            return "null";
        }
        sidList = JSON.parseArray(template.opsForValue().get(key), String.class);
        //如果有这个值
        if (sidList.contains(sid)) {
            sidList.remove(sid);
            template.opsForValue().set(key, JSON.toJSONString(sidList));
            strategyStatisVO.setFavornum(strategyStatisVO.getFavornum() - 1);
            template.opsForValue().set(token, JSON.toJSONString(strategyStatisVO));
            return null;
        } else {
            sidList.add(sid);
            template.opsForValue().set(key, JSON.toJSONString(sidList));
            strategyStatisVO.setFavornum(strategyStatisVO.getFavornum() + 1);
            template.opsForValue().set(token, JSON.toJSONString(strategyStatisVO));
            return "null";
        }

    }

    @Override
    public List<String> queryFavorNum(String sid, String userId) {
        String key = Rediskeys.FAVOR_USER_CODE.join(userId);
        if (!template.hasKey(key)) {
            return new ArrayList<>();
        }

        String s = template.opsForValue().get(key);
        List<String> list = JSON.parseArray(s, String.class);
        return list;
    }

    @Override
    public void setStatisVO(String id, StrategyStatisVO vo) {
        String token = Rediskeys.STRATEGY_STATIS_VO.join(id);
        String value = JSON.toJSONString(vo);
        template.opsForValue().set(token, value);
    }

    @Override
    public boolean hasStatisKey(String id) {
        String token = Rediskeys.STRATEGY_STATIS_VO.join(id);
        return template.hasKey(token);
    }

    @Override
    public List<StrategyStatisVO> getStatisVOs(String keyPattern) {
        Set<String> keys = template.keys(keyPattern);
        List<StrategyStatisVO> list = new ArrayList<>();
        for (String key : keys) {
            String s = template.opsForValue().get(key);
            StrategyStatisVO strategyStatisVO = JSON.parseObject(s, StrategyStatisVO.class);
            list.add(strategyStatisVO);
        }
        return list;
    }

}
